package com.jinhua.demo;

import com.jinhua.batch.model.AbstractBatchCallbackHandler;
import org.apache.commons.collections4.MapUtils;
import org.apache.poi.ss.formula.functions.T;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright
 *
 * @Author WuHuang
 * @Date 2023/4/6
 */
public class BatchActivitySpuCallbackHandler extends AbstractBatchCallbackHandler<SpuPackageDTO> {


//    PromotionPriceDependency promotionPriceDependency;
//
//    public BatchActivitySpuCallbackHandler() {
//        this.promotionPriceDependency = SpringContextUtil.getBean(PromotionPriceDependency.class);
//    }

    /**
     * 批量处理每一批数据
     * 如果抛出未捕获的异常，代表当前这批数据全部失败
     * 如果抛出了ResplitException，代表当前这批数据量太多，需要再拆分一下
     */
    @Override
    public List<SpuPackageDTO> itemBatchHandle(List<SpuPackageDTO> itemBatchList, int tryTime) {
        Map<String, SpuPackageDTO> spuIdSpuPackageDTOMap = new HashMap();
        for (SpuPackageDTO spuPackageDTO : itemBatchList) {
            spuIdSpuPackageDTOMap.put(spuPackageDTO.getOuterSpuId(), spuPackageDTO);
        }

//        Map<String, String> skuIdFailReasonMap = promotionPriceDependency.addGoods(itemBatchList);
        Map<String, String> skuIdFailReasonMap = new HashMap<>();

        // 返回处理结果，和对应的失败原因
        if (MapUtils.isEmpty(skuIdFailReasonMap)) {
            return itemBatchList;
        }
        skuIdFailReasonMap.forEach((spuId, reason) -> {
            SpuPackageDTO spuPackageDTO = spuIdSpuPackageDTOMap.get(spuId);
            spuPackageDTO.setSuccess(false);
            spuPackageDTO.setFailReason(reason);
        });

        return itemBatchList;
    }

    /**
     * 批量处理之前调用该方法
     * @param allItemList
     */
    @Override
    public void beforeHandle(List<SpuPackageDTO> allItemList) {
        super.beforeHandle(allItemList);
    }

    /**
     * 批量处理之后调用该方法
     * @param resultItemVOList
     */
//    @Override
//    public void afterHandle(List<T> resultItemVOList) {
//        super.afterHandle(resultItemVOList);
//    }


//    @Override
//    public List<FailedItemVO> itemBatchHandle(List<SpuPackageDTO> itemBatchList, int tryTime) throws ResplitException {
//        return null;
//    }

}

